package com.kantboot.functional.im.repository;

import com.kantboot.functional.im.domain.entity.FunctionalImWebsocket;
import com.kantboot.functional.im.dto.FunctionalImWebsocketSearchDTO;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * 关于websocket的服务
 */
public interface FunctionalImWebSocketRepository extends JpaRepository<FunctionalImWebsocket, Long> {

    /**
     * 根据cid查找
     */
    FunctionalImWebsocket findByCid(String cid);

    /**
     * 根据userId查找
     */
    List<FunctionalImWebsocket> findByUserId(Long userId);

    /**
     * 根据userId和isOpened查找
     */
    List<FunctionalImWebsocket> findByUserIdAndIsOpened(Long userId,Boolean isOpened);

    /**
     * 根据dto查找
     */
    @Query("""
            select t from FunctionalImWebsocket t
            where 1=1
            and (:#{#param.userId} is null or t.userId = :#{#param.userId})
            and (:#{#param.cid} is null or :#{#param.cid} = '' or t.cid = :#{#param.cid})
            and (:#{#param.isOpened} is null or t.isOpened = :#{#param.isOpened})
            and (:#{#param.isClosed} is null or t.isClosed = :#{#param.isClosed})
            and (:#{#param.lastGmtModifiedEnd} is null or t.gmtModified >= :#{#param.lastGmtModifiedEnd})
""")
    List<FunctionalImWebsocket> findByDto(
            @Param("param")FunctionalImWebsocketSearchDTO param
            );

}
